package cn.iocoder.yudao.module.shop.controller.admin.paytype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.paytype.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.paytype.PayTypeDO;
import cn.iocoder.yudao.module.shop.convert.paytype.PayTypeConvert;
import cn.iocoder.yudao.module.shop.service.paytype.PayTypeService;

@Tag(name = "管理后台 - 支付方式")
@RestController
@RequestMapping("/shop/pay-type")
@Validated
public class PayTypeController {

    @Resource
    private PayTypeService payTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建支付方式")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:create')")
    public CommonResult<Long> createPayType(@Valid @RequestBody PayTypeCreateReqVO createReqVO) {
        return success(payTypeService.createPayType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支付方式")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:update')")
    public CommonResult<Boolean> updatePayType(@Valid @RequestBody PayTypeUpdateReqVO updateReqVO) {
        payTypeService.updatePayType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支付方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:pay-type:delete')")
    public CommonResult<Boolean> deletePayType(@RequestParam("id") Long id) {
        payTypeService.deletePayType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支付方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:query')")
    public CommonResult<PayTypeRespVO> getPayType(@RequestParam("id") Long id) {
        PayTypeDO payType = payTypeService.getPayType(id);
        return success(PayTypeConvert.INSTANCE.convert(payType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得支付方式列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:query')")
    public CommonResult<List<PayTypeRespVO>> getPayTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<PayTypeDO> list = payTypeService.getPayTypeList(ids);
        return success(PayTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支付方式分页")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:query')")
    public CommonResult<PageResult<PayTypeRespVO>> getPayTypePage(@Valid PayTypePageReqVO pageVO) {
        PageResult<PayTypeDO> pageResult = payTypeService.getPayTypePage(pageVO);
        return success(PayTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付方式 Excel")
    @PreAuthorize("@ss.hasPermission('shop:pay-type:export')")
    @OperateLog(type = EXPORT)
    public void exportPayTypeExcel(@Valid PayTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayTypeDO> list = payTypeService.getPayTypeList(exportReqVO);
        // 导出 Excel
        List<PayTypeExcelVO> datas = PayTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "支付方式.xls", "数据", PayTypeExcelVO.class, datas);
    }

}
